package com.jframe.sys.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * 使用时尽量保证xml不超10M
 * @author l2iu11
 *
 */
public class XmlUtil {
	

	/**
	 * java对象转化为xml</br>
	 * @param obj
	 * @return
	 */
	public static String obj2xml(Object obj) {

		XStream xstream = new XStream(new DomDriver());
		xstream.alias(obj.getClass().getSimpleName(), obj.getClass());
		String xml = xstream.toXML(obj);// 转换�?xml 格式
		return xml;
	}

	/**
	 * java对象数组转化为xml</br>
	 * @param obj
	 * @param subClzs
	 * @return
	 */
	public static String obj2Xml(Object obj, Class[] subClzs) {
		
		XStream xstream = new XStream(new DomDriver());
		xstream.alias(obj.getClass().getSimpleName(), obj.getClass());
		for (Class clz : subClzs) {
			xstream.alias(clz.getSimpleName(), clz);
		}
		return xstream.toXML(obj);
	}
	
	/**
	 * xml转化为Java对象
	 * xml不超10M建议使用
	 * @param xml
	 * @param clz
	 * @return
	 */
	public static Object xml2obj(String xml, Class clzz) {

		XStream xstream = new XStream(new DomDriver());
		xstream.alias(clzz.getSimpleName(), clzz);
		return xstream.fromXML(xml);
	}
	
	/*
	public static void main(String args[]){
		List<User> list = new ArrayList<User>();
		User user1 = new User();
		user1.setUserNo("2222111");
		list.add(user1);
		User user = new User();
		user.setUserNo("2222");
		list.add(user);
		System.out.println(obj2xml(list));
		List<User> list1 = (List<User>) xml2obj(obj2xml(list),List.class);
		System.out.println(list1.size());
	}*/
	
}
